import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

const String STATIC_STRING_FROM_CAMERA = "拍摄";
const String STATIC_STRING_FROM_PHOTO_LIBRARY = "从相册中选择";
const String STATIC_STRING_CANCEL = "取消";

class ImagePickerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ImagePickerPageState();
  }
}

class ImagePickerPageState extends State<ImagePickerPage> {
  File _image;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            padding: EdgeInsets.all(16),
            height: 200,
            child: InkWell(
                onTap: () => addImage(
                    context,
                    (file) => setState(() {
                          _image = file;
                        })),
                child: Center(
                    child: _image == null
                        ? Container(color: Colors.grey)
                        : Image.file(_image)))));
  }
}

void addImage(BuildContext context, Function(File) callback) {
  showModalBottomSheet(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(16), topRight: Radius.circular(16))),
      context: context,
      builder: (context) => ImagePickerPopupView(
            onSelectImage: (file) {
              callback(file);
            },
          ));
}

enum SelectImageType { none, camera, photoLib }

class ImagePickerPopupView extends StatelessWidget {
  ImagePickerPopupView({Key key, this.onSelectImage}) : super(key: key);

  final Function(File) onSelectImage;

  final ImagePicker _picker = ImagePicker();

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 200,
        child: Column(
          children: [
            Expanded(
                child: Material(
                    color: Colors.transparent,
                    child: InkWell(
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(16),
                            topRight: Radius.circular(16)),
                        onTap: () {
                          getImage(context, SelectImageType.camera);
                        },
                        child: Padding(
                            padding: EdgeInsets.only(top: 16, bottom: 16),
                            child: Center(
                                child: Text(
                              STATIC_STRING_FROM_CAMERA,
                              style: TextStyle(
                                  fontWeight: FontWeight.normal, fontSize: 16),
                            )))))),
            Divider(
              height: 1,
            ),
            Expanded(
                child: Material(
                    color: Colors.transparent,
                    child: InkWell(
                        onTap: () {
                          getImage(context, SelectImageType.photoLib);
                        },
                        child: Padding(
                            padding: EdgeInsets.only(top: 16, bottom: 16),
                            child: Center(
                                child: Text(
                              STATIC_STRING_FROM_PHOTO_LIBRARY,
                              style: TextStyle(
                                  fontWeight: FontWeight.normal, fontSize: 16),
                            )))))),
            Container(
              color: Colors.grey,
              height: 8,
            ),
            Divider(
              height: 1,
            ),
            Expanded(
                child: Material(
                    color: Colors.transparent,
                    child: InkWell(
                        onTap: () {
                          getImage(context, SelectImageType.none);
                        },
                        child: Padding(
                            padding: EdgeInsets.only(top: 16, bottom: 16),
                            child: Center(
                                child: Text(
                              STATIC_STRING_CANCEL,
                              style: TextStyle(
                                  fontWeight: FontWeight.normal, fontSize: 16),
                            )))))),
          ],
        ));
  }

  Future getImage(BuildContext context, SelectImageType type) async {
    switch (type) {
      case SelectImageType.camera:
        final pickedFile = await _picker.getImage(source: ImageSource.camera);
        if (pickedFile != null) {
          onSelectImage(File(pickedFile.path));
        }

        break;
      case SelectImageType.photoLib:
        final pickedFile = await _picker.getImage(source: ImageSource.gallery);
        if (pickedFile != null) {
          onSelectImage(File(pickedFile.path));
        }
        break;
      case SelectImageType.none:
        break;
    }

    Navigator.pop(context);
  }
}
